module base {
    /**
     * 全局数据
     * @author none
     *
     */
    export class AppData extends how.module.Data 
    {
        private static _instance: AppData;
        public constructor() 
        {
            super();
        }

        public static getInstance(): AppData 
        {
            if (!this._instance) 
            {
                this._instance = new AppData();
            }
            return this._instance;
        }
        
        public language: string = "zh-cn";//默认中文
        public currentGameIndex: number = 0;//当前游戏
        public currentSocket: number = -1;//现在链接的服务器：0：游戏大厅服务器，1：游戏服务器
        public account: string = "";//当前帐号
        public token: string = "";//当前token

        // public agentId: number;//渠道ID，只用于与服务器数据交互
        public userData: base.UserData = new base.UserData();
        public roomList: Array<RoomData> = new Array<RoomData>();//所有房间列表
        public gameList: Array<GameData> = new Array<GameData>();//游戏列表
        public kindClassMap = new Array(); // 游戏对应类名的字典
        public lastServerId: number = 0;//最后的服务器id(断线重连)
        public lastGameId: number = 0;//最后的游戏id(断线重连)
        public lastServerPort: number = 0;//最后的服务器的端口号
        public lastPreScore: number = 0;//最后进入房间的前注
        public currentRoom: RoomData;//当前房间信息
        public currentGame: GameData;//当前游戏信息
        public isGameHide: boolean = false;//游戏是否进入后台
        public cardRoomData: CardRoomData = null; // 房卡房间数据;
        /**
        * 游戏数据
        */
        public get gameData(): GameSceneGlobalData 
        {
            var gameData: GameSceneGlobalData = null;
            var kindClassString: string = this.kindClassMap[AppData.getInstance().currentGame.id];
            if(kindClassString)
            {
                if(kindClassString.indexOf(",") != -1)
                {
                    var kindClass = kindClassString.split(",");
                    gameData = egret.getDefinitionByName(kindClass[2].trim()).getInstance();
                }
            }
            else
            {
                console.log(">>> 改KindID还未配置对应的class,请联系服务器配置, KindID:" + AppData.getInstance().currentGame.id);
            }

            return gameData;
        }

        //数据初始化
        public dataInit(): void 
        {
            this.gameList = [];
            this.roomList = [];
            this.currentRoom = null;
        }


        /**
         * 设置房间列表数据
         */
        // "wKindID":[5,5,104,205,3,203]
        // "wServerID":[3500,3540,3400,3300,3200,3100]
        // "T":[2,32,2,2,2,2]
        // "szServerName":["新手","房卡房","百人牛牛","炸金花","十三水","斗地主"]
        // "lBaseScore":[1000,0,100,100,100,100]
        // "lLimitScore":[0,0,0,0,0,0]
        // "lCellScore":[50,0,100,10,10,10]
        public setRoomList(source: any): void 
        {
            this.roomList = [];
            if(source)
            {
                for(var i = 0; i < source.wKindID.length; i++)
                {
                    var roomData: RoomData = new RoomData();
                    roomData.kindID = source.wKindID[i];
                    roomData.id = source.wServerID[i];
                    roomData.port = source.wServerID[i];
                    roomData.type = source.T[i];
                    roomData.name = source.szServerName[i];
                    roomData.baseScore = source.lBaseScore[i];
                    roomData.limitScore = source.lLimitScore[i];
                    roomData.cellScore = source.lCellScore[i];
                    this.roomList.push(roomData);
                }
            }

            this.roomList.sort(function (a: any, b: any): number 
            {
                return a.baseScore < b.baseScore ? -1 : 1;
            });
        }

        //收到游戏列表
        // "wKindID":[5,5,104,205,3,203]
        // "wServerID":[3500,3540,3400,3300,3200,3100]
        // "T":[2,32,2,2,2,2]
        // "szServerName":["新手","房卡房","百人牛牛","炸金花","十三水","斗地主"]
        // "lBaseScore":[1000,0,100,100,100,100]
        // "lLimitScore":[0,0,0,0,0,0]
        // "lCellScore":[50,0,100,10,10,10]
        public setGameList(data: any): void 
        {
            this.gameList = [];
            if (data)
            {
                for (var i = 0; i < data.wKindID.length; i++)
                 {
                    var kindID = data.wKindID[i];
                    if(this.gameList.filter(value => value.id == kindID).length == 0)
                    {
                        var gameData: GameData = new GameData();
                        gameData.id = kindID;
                        this.gameList.push(gameData);
                    }
                 }
            }
        }

        public setKindClassName(data: any): void
        {
            for(var kind in data.C)
            {
                this.kindClassMap[parseInt(kind)] = data.C[kind];
            }
            // 测试代码 
            console.log(">>>>>> 测试代码开始 >");
            this.kindClassMap[104] = "P100GameSceneViewNew";
            this.kindClassMap[203] = "DDZGameSceneView";
            console.log(">>>>>> 测试代码结束 <");
        }

        //根据级别和顺序获取房间数据
        private getRoomData(level: number, sortID: number): any 
        {
            var reData: any = null;
            var roomData: RoomData;
            for (var i = 0; i < this.roomList.length; i++) 
            {
                roomData = this.roomList[i];
                if (parseInt(roomData.name) == level * 10 + sortID + 1) 
                {
                    return roomData;
                }
            }
            return null;
        }


        //获取渠道id
        public getGameChannelId() 
        {
            for (var i = 0, gameList = this.gameList, len = gameList.length; i < len; i++) 
            {
                var game = gameList[i];
                if (game.id == this.currentGameIndex)
                {
                    return game.channelID;
                }
            }
        }

        //获取游戏信息
        public getGameById(id: number) 
        {
            for (var i = 0, gameList = this.gameList, len = gameList.length; i < len; i++) 
            {
                if (gameList[i].id == id)
                {
                    return gameList[i];
                }
            }
        }

        //设置语言
        public setLanguage(language: string): void 
        {
            this.language = language.toLowerCase();
        }
    }
}